package com.myapplication.ui.homefragment.shoefragment;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapplication.R;
import com.myapplication.bean.Shop;
import com.myapplication.ui.myactivity.MyCollection;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by zitaojiang on 2016/10/8.
 */
public class ShoesAdapter extends BaseAdapter {
    private List<ShoeBean.RowsBean> list;
    private Context context;
    private MyCollection myCollection=new MyCollection();

   

    public List<ShoeBean.RowsBean> getList() {
        return list;
    }

    public void setList(List<ShoeBean.RowsBean> list) {
        this.list = list;
    }

    public ShoesAdapter(Context context, List<ShoeBean.RowsBean> list){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return  list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder holder=null;

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        holder=new ViewHolder();

            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.item_shop, null);
                holder.iv = (ImageView) view.findViewById(R.id.iv);
                holder.tv_intro = (TextView) view.findViewById(R.id.tv_intro);
                holder.tv_newprice = (TextView) view.findViewById(R.id.tv_newprice);
                holder.tv_oldprice = (TextView) view.findViewById(R.id.tv_oldprice);
                holder.tv_count = (TextView) view.findViewById(R.id.tv_count);
                holder.tv_send = (TextView) view.findViewById(R.id.tv_send);
                holder.button= (Button) view.findViewById(R.id.btn_collect);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.tv_intro.setText(list.get(position).getName());
/*if (list.get(position).getNewPrice()!=null){
    holder.tv_newprice.setText("¥"+list.get(position).getNewPrice());
}else {
    holder.tv_newprice.setText("¥");
}
        if (list.get(position).getNewPrice()!=null){
            holder.tv_oldprice.setText("¥"+list.get(position).getOldPrice());

        }else {
            holder.tv_newprice.setText("1¥");
        }*/
        if (list.get(position).getNewPrice().length()>3){
            holder.tv_newprice.setText("¥"+list.get(position).getNewPrice().substring(0,4));
        }else {
            holder.tv_newprice.setText("¥"+list.get(position).getNewPrice());
        }
        if (list.get(position).getOldPrice().length()>3){
            holder.tv_oldprice.setText("¥"+list.get(position).getOldPrice().substring(0,4));
        }else {
            holder.tv_oldprice.setText("¥"+list.get(position).getOldPrice());
        }
            holder.tv_oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.tv_count.setText("已售:"+list.get(position).getSaleTotal()+"");
            holder.tv_send.setText("包邮");

            Picasso.with(context)
                    .load(list.get(position).getProductImg()).fit()
                    .into(holder.iv);

        //收藏的点击事件
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获得登陆状态
                BmobUser user=BmobUser.getCurrentUser();

                //如果登陆，则进行收藏
                if (user!=null){
                    String mName=user.getUsername();
                    myCollection.setMyName(mName);
                    myCollection.setPic(list.get(position).getProductImg());
                    myCollection.setIntro(list.get(position).getName());
                    myCollection.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(context,"收藏成功",Toast.LENGTH_LONG).show();

                            } else {
                                Toast.makeText(context,"收藏失败，请检查是否连接网络",Toast.LENGTH_LONG).show();

                            }
                        }
                    });
                }
                //没有登陆，则提醒用户登陆
                else {
                    Toast.makeText(context,"请先登陆",Toast.LENGTH_SHORT).show();

                }
                //name=user.getUsername();


            }
        });
        return view;
        }

    static class ViewHolder{
        ImageView iv;
        TextView tv_intro,tv_newprice,tv_oldprice,tv_count,tv_send;
        Button button;
    }
}
